﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * HTTP运行时缓存提供者。
 * 
 * 最后修改：2011-10-28
 * ************************************************************/
namespace Mozlite.Caching
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.Caching;
    using System.Collections;
    /// <summary>
    /// HTTP运行时缓存提供者。
    /// </summary>
    public class HttpRuntimeCacheProvider : CacheProviderBase
    {
        private readonly Cache cache = HttpRuntime.Cache;
        /// <summary>
        /// 清楚所有缓存。
        /// </summary>
        public override void Clear()
        {
            IDictionaryEnumerator enumerator = this.GetEnumerator();
            while (enumerator.MoveNext())
            {
                this.Remove((string)enumerator.Key);
            }
        }
        /// <summary>
        /// 检索缓存。
        /// </summary>
        /// <param name="key">缓存键。</param>
        /// <returns>检索到的缓存项，未找到该键时为 null。</returns>
        public override object Get(string key)
        {
            return this.cache[key];
        }
        /// <summary>
        /// 检索用于循环访问包含在缓存中的键设置及其值的字典枚举数。
        /// </summary>
        /// <returns>返回要循环访问<see cref="T:System.Web.Caching.Cache"/>对象的枚举数。</returns>
        public override IDictionaryEnumerator GetEnumerator()
        {
            return this.cache.GetEnumerator();
        }
        /// <summary>
        /// 插入缓存。
        /// </summary>
        /// <param name="key">缓存键。</param>
        /// <param name="value">插入对象实例。</param>
        /// <param name="dependencies">缓存依赖项。</param>
        /// <param name="absoluteExpiration">绝对过期日期。</param>
        /// <param name="slidingExpiration">相对过期时间。</param>
        /// <param name="priority">缓存优先级。</param>
        /// <param name="onRemoveCallback">当移除时回调函数。</param>
        public override void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
        {
            if (value == null)
                return;
            CacheSettingsAttribute cacheSettings = CacheProviderBase.GetCacheSettings(value);
            bool cachable = cacheSettings != null ? cacheSettings.AllowHttpCacheProviderUsage : true;
            if (cachable)
                this.cache.Insert(key, value, dependencies, absoluteExpiration, slidingExpiration, (CacheItemPriority)priority, onRemoveCallback);
        }
        /// <summary>
        /// 移除缓存键。
        /// </summary>
        /// <param name="key">缓存键。</param>
        public override void Remove(string key)
        {
            this.cache.Remove(key);
        }

        /// <summary>
        /// 获取提供者名称。
        /// </summary>
        public override string ProviderName
        {
            get
            {
                return "HttpRuntimeCacheProvider";
            }
        }
    }
}
